package servlet;

import common.framework.simplemvc.RequestMapping;
import common.utils.JSONUtil;
import common.utils.Result;
import entity.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("/user")
public class UserServlet {

    // 控制层 -》业务层-》持久层

    UserService userService = new UserService();

    @RequestMapping("/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("insert.do");

        return "hello";
    }

    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("update.do");
    }

    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("delete.do");
    }

    @RequestMapping("/select.do")
    public void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("select.do");

        List<User> list = userService.list();
        // 返回json
        JSONUtil.printByJSON(resp, Result.success(list));
    }
}
